<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="handleInput"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="depts"
          node-key="id"
          :props="{label: 'name'}"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          @current-change="handleCurrentChange"
        />
      </div>

      <div class="right">
        <!-- type="flex" 不写 => 就不是flex布局 => 老的float布局  -->
        <!-- type="flex" 开启flex布局 -->
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('employee/detail')">添加员工</el-button>

          <el-button size="mini" @click="showDialog=true">excel导入</el-button>

          <el-button size="mini" :loading="loading" @click="getFileData">excel导出</el-button>

        </el-row>

        <!-- 表格组件 -->
        <el-table :data="rows">
          <el-table-column align="center" label="头像" prop="staffPhoto">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <span v-else class="hhh">{{ row.username.slice(2,3) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="手机号" sortable prop="mobile" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              <el-tag v-if="row.formOfEmployment === 1" type="success">正式</el-tag>
              <el-tag v-else type="danger">试用</el-tag>

            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>

              <el-button size="mini" type="text">角色</el-button>

              <el-button size="mini" type="text">删除</el-button>

            </template>

          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="center">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="page"
            @current-change="handlepageChange"
          />
        </el-row>

      </div>
      <ImportExcel v-model="showDialog" @reload="getEmployeeList" />
    </div>

  </div>

</template>

<script>
import { getDepartments } from '@/api/department'
import { transList2TreeData } from '@/utils'
import { getEmployeeList, getFileData } from '@/api/employee'
import fileSaver from 'file-saver'
import ImportExcel from '@/views/employee/components/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      loading: false,
      depts: [],
      selectId: null,
      total: 0,
      rows: [],
      page: 1,
      keyword: '',
      showDialog: false
      // timer: null
      // data中的数据什么需要提供?什么不需要提供?
      // 不需要提供的数据: 通过计算属性计算出来的数据
      // 需要提供的数据: 页面中需要展示的数据 会随着用户操作而改变的数据

    }
  },
  created() {
    this.getDepts()
  },
  methods: {
    async getFileData() {
      try {
        this.loading = true
        const res = await getFileData()
        this.loading = false
        fileSaver.saveAs(res, '员工数据.xlsx')
      } catch (error) {
        // 提示
        this.$message.error('下载失败')
      } finally {
        // 不管成功还是失败都会执行
        this.loading = false
      }
    },
    handleInput() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.page = 1
        this.getEmployeeList()
      }, 500)
    },
    handlepageChange(page) {
      this.page = page
      this.getEmployeeList()
    },
    async getDepts() {
      const res = await getDepartments()

      // 将数据转换为树形结构
      this.depts = transList2TreeData(res, 0)
      // 默认选中第一个节点
      this.selectId = this.depts[0].id
      // 需要等待渲染完成之后才能高亮
      this.$nextTick(() => {
        // 高亮第一个节点setCurrentKey需要配合node-key使用
        this.$refs.tree.setCurrentKey(this.selectId)
      })
      this.getEmployeeList()
    },
    handleCurrentChange(obj) {
      console.log('有人选了一个节点', obj)
      // 记录id  将来配合右侧进行数据展示
      this.selectId = obj.id
      // 重新获取员工列表
      this.page = 1
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList(
        {
          departmentId: this.selectId,
          page: this.page,
          pagesize: 5,
          keyword: this.keyword
        }
      )
      this.total = total
      this.rows = rows
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
    .hhh {
  display: inline-block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #8df;

  text-align: center;
  line-height: 40px;
  color: #fff;
  font-size: 20px;
}
  }
}

</style>
